# -*- coding:utf-8 -*-
"""
该脚本对选择的物体设置关键帧，控制mesh_light的闪烁频率
"""

import sys
from PySide2.QtGui import *
from PySide2.QtCore import *
from PySide2.QtWidgets import *
try:
    import maya.cmds as cmds
    import shiboken2 as shiboken
    import maya.OpenMayaUI as omui
except Exception as test_ERR:
    print('test')


def maya_main_window():
    main_window_ptr = omui.MQtUtil.mainWindow()
    return shiboken.wrapInstance(long(main_window_ptr), QWidget)

class LightControl(QWidget):

    def __init__(self,parent=None):
        super(LightControl, self).__init__(parent)

        self.big_font = QFont("",14,65)
        self.mid_font = QFont('',10,65)
        intLimit = QIntValidator(0, 9999, self)
        self.btn_style = """QPushButton{color:black}
                                  QPushButton:hover{color:red}
                                 QPushButton{background-color:#A9A9A9}
                                  QPushButton{border:2px}
                                  QPushButton{border-radius:3px}
                                  QPushButton{padding:1px 1px}"""
        self.setWindowTitle(u'救护车灯')
        self.setWindowFlags(Qt.WindowStaysOnTopHint | Qt.Window)
        self.main_layout = QVBoxLayout()
        self.main_layout.setAlignment(Qt.AlignTop)
        self.main_layout.setSpacing(25)
        self.show_label = QLabel(u'救护灯频率设置')
        self.show_label.setFont(self.big_font)
        self.show_label.setAlignment(Qt.AlignCenter)

        self.main_lamp_label = QLabel(u'主灯设置')
        self.main_lamp_label.setFont(self.mid_font)
        self.main_lamp_label.setAlignment(Qt.AlignCenter)
        self.frame_layout = QHBoxLayout()
        self.frame_label = QLabel(u'总帧数范围  :')
        self.frame_edit = QLineEdit()
        self.frame_edit.setText('1000')
        self.frame_edit.setValidator(intLimit)
        self.frame_layout.addWidget(self.frame_label)
        self.frame_layout.addWidget(self.frame_edit)

        self.base_layout = QHBoxLayout()
        self.base_label = QLabel(u'基本频率    :')
        self.base_edit = QLineEdit()
        self.base_edit.setValidator(intLimit)
        self.base_edit.setText('25')
        self.base_layout.addWidget(self.base_label)
        self.base_layout.addWidget(self.base_edit)

        self.base_open_frame_layout = QHBoxLayout()
        self.base_open_label = QLabel(u'亮灯时长    :')
        self.base_open_edit = QLineEdit()
        self.base_open_edit.setValidator(intLimit)
        self.base_open_edit.setText('3')
        self.base_open_frame_layout.addWidget(self.base_open_label)
        self.base_open_frame_layout.addWidget(self.base_open_edit)

        self.common_layout = QHBoxLayout()
        self.common_label = QLabel(u'共闪触发间隔:')
        self.common_edit = QLineEdit()
        self.common_edit.setText('100')
        self.common_edit.setValidator(intLimit)
        self.common_layout.addWidget(self.common_label)
        self.common_layout.addWidget(self.common_edit)

        self.duration_layout = QHBoxLayout()
        self.duration_label = QLabel(u'共闪持续时长:')
        self.duration_edit = QLineEdit()
        self.duration_edit.setText('48')
        self.duration_edit.setValidator(intLimit)
        self.duration_layout.addWidget(self.duration_label)
        self.duration_layout.addWidget(self.duration_edit)

        self.alternat_layout = QHBoxLayout()
        self.alternat_label = QLabel(u'共闪交替时长:')
        self.alternat_edit = QLineEdit()
        self.alternat_edit.setValidator(intLimit)
        self.alternat_edit.setText('8')
        self.alternat_layout.addWidget(self.alternat_label)
        self.alternat_layout.addWidget(self.alternat_edit)

        self.main_lamp_btn = QPushButton(u'设置')
        self.main_lamp_btn.setMinimumHeight(25)
        self.main_lamp_btn.setStyleSheet(self.btn_style)
        self.main_lamp_btn.clicked.connect(self._set_main_lamp)

        self.bg_lamp_label = QLabel(u'副灯设置')
        self.bg_lamp_label.setContentsMargins(0,20,0,0)
        self.bg_lamp_label.setFont(self.mid_font)
        self.bg_lamp_label.setAlignment(Qt.AlignCenter)
        self.frequency_layout = QHBoxLayout()
        self.frequency_label = QLabel(u'频率:    ')
        self.frequency_edit = QLineEdit()
        self.frequency_edit.setText('3')
        self.frequency_edit.setValidator(intLimit)
        self.frequency_layout.addWidget(self.frequency_label)
        self.frequency_layout.addWidget(self.frequency_edit)
        self.bg_duration_layout = QHBoxLayout()
        self.bg_label = QLabel(u'亮灯帧长:')
        self.bg_edit = QLineEdit()
        self.bg_edit.setText('3')
        self.bg_edit.setValidator(intLimit)
        self.bg_duration_layout.addWidget(self.bg_label)
        self.bg_duration_layout.addWidget(self.bg_edit)
        self.frequency_btn = QPushButton(u'设置')
        self.frequency_btn.setMinimumHeight(25)
        self.frequency_btn.setStyleSheet(self.btn_style)
        self.frequency_btn.clicked.connect(self._set_bg_lamp)

        self.main_layout.addWidget(self.show_label)
        self.main_layout.addWidget(self.main_lamp_label)
        self.main_layout.addLayout(self.frame_layout)
        self.main_layout.addLayout(self.base_layout)
        self.main_layout.addLayout(self.base_open_frame_layout)
        self.main_layout.addLayout(self.common_layout)
        self.main_layout.addLayout(self.duration_layout)
        self.main_layout.addLayout(self.alternat_layout)
        self.main_layout.addWidget(self.main_lamp_btn)
        self.main_layout.addWidget(self.bg_lamp_label)
        self.main_layout.addLayout(self.frequency_layout)
        self.main_layout.addLayout(self.bg_duration_layout)
        self.main_layout.addWidget(self.frequency_btn)
        
        self.setLayout(self.main_layout)

    def setKey(self,obj,index):
        frames = int(self.frame_edit.text())
        base = int(self.base_edit.text())
        base_frame = int(self.base_open_edit.text())
        common = int(self.common_edit.text())
        duration = int(self.duration_edit.text())
        alternat = int(self.alternat_edit.text())
        print(frames,base,common,duration,alternat)
        cmds.cutKey(obj,t=(0,frames),at='lightVisible',option='keys')
        shape = cmds.listRelatives(obj, shapes=1)[0]
        ori_intensity = cmds.getAttr(shape+'.intensity')
        ori_aiExposure = cmds.getAttr(shape+'.aiExposure')
        for i in range(frames):
            r = (i-index*5) % base
            big_c = i % common
            inner_c = i % 3
            diff_c = (i+alternat) % (alternat*2) if index in [0,2,5] else i % (alternat*2)
            if big_c > duration:
                # if r in [0, 1, 3, 4]:
                if r <= base_frame:
                    cmds.setKeyframe(obj, v=1, at='lightVisible', t=[i])
                    cmds.setKeyframe(obj, v=ori_intensity, at='intensity', t=[i])
                    cmds.setKeyframe(obj, v=ori_aiExposure, at='aiExposure', t=[i])
                else:
                    cmds.setKeyframe(obj, v=0, at='lightVisible', t=[i])
                    cmds.setKeyframe(obj, v=0, at='intensity', t=[i])
                    cmds.setKeyframe(obj, v=0, at='aiExposure', t=[i])
            else:
                if inner_c == 0 and diff_c > alternat:
                    cmds.setKeyframe(obj, v=1, at='lightVisible', t=[i])
                    cmds.setKeyframe(obj, v=ori_intensity, at='intensity', t=[i])
                    cmds.setKeyframe(obj, v=ori_aiExposure, at='aiExposure', t=[i])
                else:
                    cmds.setKeyframe(obj, v=0, at='lightVisible', t=[i])
                    cmds.setKeyframe(obj, v=0, at='intensity', t=[i])
                    cmds.setKeyframe(obj, v=0, at='aiExposure', t=[i])

    def _set_main_lamp(self):
        """
        设置主灯频率
        :return:
        """

        sn = cmds.ls(sl=True)
        for i in range(len(sn)):
            shape = cmds.listRelatives(sn[i],shapes=1)[0]
            cmds.setAttr('{}.aiTranslator'.format(shape),'mesh_light',type='string')
            self.setKey(sn[i],i)


    def flash_object(self,obj,ori_intensity,ori_aiExposure):
        """
        闪烁物体
        :param obj:
        :return:
        """
        frames = int(self.frame_edit.text())
        frequency = int(self.frequency_edit.text())
        bg_duration = int(self.bg_edit.text())
        for i in range(frames):
            if i % frequency <= bg_duration:
                cmds.setKeyframe(obj, v=1, at='lightVisible', t=[i])
                cmds.setKeyframe(obj, v=ori_intensity, at='intensity', t=[i])
                cmds.setKeyframe(obj, v=ori_aiExposure, at='aiExposure', t=[i])
            else:
                cmds.setKeyframe(obj, v=0, at='lightVisible', t=[i])
                cmds.setKeyframe(obj, v=0, at='intensity', t=[i])
                cmds.setKeyframe(obj, v=0, at='aiExposure', t=[i])

    def _set_bg_lamp(self):
        """
        设置背景灯频率
        :return:
        """
        sn = cmds.ls(sl=True)
        for i in sn:
            shape = cmds.listRelatives(sn,shapes=1)[0]
            ori_intensity = cmds.getAttr(shape + '.intensity')
            ori_aiExposure = cmds.getAttr(shape + '.aiExposure')
            cmds.setAttr('{}.aiTranslator'.format(shape),'mesh_light',type='string')
            self.flash_object(i,ori_intensity,ori_aiExposure)





def main():
    global win
    try:
        # win.close()
        win.setHidden(False)
    except:
        pass
    win = LightControl(parent=maya_main_window())
    win.show()
    
if __name__ == '__main__':
    # app = QApplication(sys.argv)
    l = LightControl()
    l.show()
    # sys.exit(app.exec_())
        
        
        
        
        
        
        
        
        
        